package com.zhuge_new.Activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.pinball83.maskededittext.MaskedEditText;
import com.maning.mndialoglibrary.MToast;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.BankInfoBean;
import com.zhuge_new.untils.CardUtil;

/**
 * Created by lenovo on 2018-08-30.
 */

public class CreditAddition1Activity extends BaseActivity implements View.OnClickListener {

    private EditText et_import;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creditadd1ition);
        init_title();
    }

    public void init_title() {
        TextView title = findViewById(R.id.tv_nav_title);
        TextView tv_select = findViewById(R.id.tv_select);
        /*et_card = findViewById(R.id.et_card);*/
        final TextView tv_card = findViewById(R.id.tv_card);
        ImageView iv_nav_back = findViewById(R.id.iv_nav_back);
        et_import = findViewById(R.id.et_import);
        TextView next_step = findViewById(R.id.next_step);
        title.setText(R.string.fund_xiaoyan);
        tv_select.setOnClickListener(this);
        et_import.setOnClickListener(this);
        next_step.setOnClickListener(this);
        iv_nav_back.setOnClickListener(this);
        et_import.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String etn = s.toString();
                String splitStr = etn.replaceAll("\\d{4}(?!$)", "$0 ");
                tv_card.setText(splitStr);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popOneActivity(CreditAddition1Activity.class);
                break;
            case R.id.tv_select:

                break;
            case R.id.et_card:
                break;
            case R.id.et_import:
                break;
            case R.id.next_step:
                calculate_card();
                break;
        }
    }

    private void calculate_card() {
        String bankname;
        String cardtype;
        String card = et_import.getText().toString();
        if (card != null && CardUtil.checkBankCard(card)) {
            BankInfoBean bankinfobean = new BankInfoBean(card);
            bankname = bankinfobean.getBankName();
            cardtype = bankinfobean.getCardType();
            if (cardtype.equals("信用卡")) {
                Intent intent = new Intent(CreditAddition1Activity.this, CreditAddition2Activity.class);
                intent.putExtra("bank_number", card);
                intent.putExtra("bankname", bankname + cardtype);
                startActivity(intent);
            } else {
                MToast.makeTextShort(CreditAddition1Activity.this, "请输入信用卡卡号").show();
            }
        } else {
            MToast.makeTextShort(CreditAddition1Activity.this, "卡号" + card + "输入不合法，请重新输入").show();
        }
    }


}
